# import os
# from openpyxl import load_workbook
# from openpyxl.workbook import Workbook
# from openpyxl.styles import Alignment,Border,Side,Font,PatternFill,Color
# import tkinter as tk
# #
#
# #追加数据
# BORDER=Border(
#      top=Side(style="thin",color="000000"),
#      bottom=Side(style="thin",color="000000"),
#      left=Side(style="thin",color="000000"),
#      right=Side(style="thin",color="000000")
# )
# def write_excel(接单日期,报告编号,单位名称,接样日期,出单日期,样品类型,检测因子,检测人员):
#     file_path =os.path.join("files","2025年台账.xlsx")
#     if os.path.exists(file_path):
#         wb=load_workbook(file_path)
#     else:
#         wb=Workbook()
#     ws=wb.worksheets[0]
#     first_row_data=ws.cell(1,1).value
#     if first_row_data:
#         row_index=ws.max_row+1
#     else:
#         row_index=1
#     ws.cell(row_index,1).value=接单日期
#     ws.cell(row_index,2).value=报告编号
#     ws.cell(row_index,3).value=单位名称
#     ws.cell(row_index,4).value=接样日期
#     ws.cell(row_index,5).value=出单日期
#     ws.cell(row_index,6).value=样品类型
#     ws.cell(row_index,7).value=检测因子
#     ws.cell(row_index,8).value=检测人员
#     for i in range(1,9):
#         ws.cell(row_index, i).border = BORDER
#         ws.cell(row_index, i).alignment=Alignment(horizontal="center",vertical="center")
#         ws.cell(row_index, i).font=Font(color="000000", sz=12, name="楷体")
#     if row_index % 2 == 1:
#         for j in range(1,9):
#             ws.cell(row_index,j).fill=PatternFill("solid",fgColor="87CEFA")
#     wb.save(file_path)
# def run():
#     while True:
#         接单日期 = input("接单日期：")
#         报告编号 = input("报告编号：")
#         单位名称 = input("单位名称：")
#         接样日期 = input("接样日期：")
#         出单日期 = input("出单日期：")
#         样品类型 = input("样品类型：")
#         检测因子 = input("检测因子：")
#         检测人员 = input("检测人员：")
#         write_excel(接单日期,报告编号,单位名称,接样日期,出单日期,样品类型,检测因子,检测人员)
#
# if __name__ == '__main__':
#     run()


#删除行
# file_path=os.path.join(r"C:\Users\10313\Desktop","仪器使用.xlsx")
# wb=load_workbook(file_path)
# ws=wb.worksheets[0]
# ws.delete_rows(idx=4,amount=5)
# wb.save("files/删除行.xlsx")

#删除列
# file_path=os.path.join(r"C:\Users\10313\Desktop","仪器使用.xlsx")
# wb=load_workbook(file_path)
# ws=wb.worksheets[0]
# ws.delete_cols(idx=5,amount=2)
# wb.save("files/删除列.xlsx")

#插入行
# file_path=os.path.join(r"C:\Users\10313\Desktop","仪器使用.xlsx")
# wb=load_workbook(file_path)
# ws=wb.worksheets[0]
# ws.insert_rows(idx=6,amount=5)
# wb.save("files/插入行.xlsx")

#插入列
# file_path=os.path.join(r"C:\Users\10313\Desktop","仪器使用.xlsx")
# wb=load_workbook(file_path)
# ws=wb.worksheets[0]
# ws.insert_cols(idx=3,amount=3)
# wb.save("files/插入列.xlsx")



# 定义样式
import os
from openpyxl import load_workbook, Workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill
import tkinter as tk

# 定义样式
BORDER = Border(
    top=Side(style="thin", color="000000"),
    bottom=Side(style="thin", color="000000"),
    left=Side(style="thin", color="000000"),
    right=Side(style="thin", color="000000")
)

def write_excel(data):
    file_path = os.path.join("files", "2025年台账.xlsx")
    if not os.path.exists(file_path):
        wb = Workbook()
        ws = wb.active
        # 如果文件不存在，则添加表头
        headers = ["接单日期", "报告编号", "单位名称", "接样日期", "出单日期", "样品类型", "检测因子", "检测人员"]
        for col_num, header in enumerate(headers, 1):
            cell = ws.cell(row=1, column=col_num)
            cell.value = header
            cell.font = Font(bold=True)
            cell.fill = PatternFill("solid", fgColor="D3D3D3")
            cell.border = BORDER
            cell.alignment = Alignment(horizontal="center", vertical="center")
    else:
        wb = load_workbook(file_path)
        ws = wb.worksheets[0]

    row_index = ws.max_row + 1
    for col_num, value in enumerate(data, 1):
        cell = ws.cell(row=row_index, column=col_num)
        cell.value = value
        cell.border = BORDER
        cell.alignment = Alignment(horizontal="center", vertical="center")
        cell.font = Font(color="000000", sz=12, name="楷体")

    if row_index % 2 == 1:
        for col_num in range(1, len(data) + 1):
            ws.cell(row=row_index, column=col_num).fill = PatternFill("solid", fgColor="87CEFA")

    wb.save(file_path)

def submit(entries):
    data = [entry.get() for entry in entries]
    write_excel(data)
    # 清空输入框
    for entry in entries:
        entry.delete(0, 'end')

# 创建主窗口
root = tk.Tk()
root.title("Excel 数据录入")

labels = ["接单日期", "报告编号", "单位名称", "接样日期", "出单日期", "样品类型", "检测因子", "检测人员"]
entries = []

for idx, label_text in enumerate(labels, start=0):
    tk.Label(root, text=label_text).grid(row=idx, column=0, padx=10, pady=5)
    entry = tk.Entry(root)
    entry.grid(row=idx, column=1, padx=10, pady=5)
    entries.append(entry)

submit_button = tk.Button(root, text="提交", command=lambda: submit(entries))
submit_button.grid(row=len(labels), columnspan=2, pady=10)

# 启动主循环
if __name__ == '__main__':
    root.mainloop()